package com.bupt.ilink.controller;

import com.bupt.ilink.entity.User;
import com.bupt.ilink.entity.pojo.store.QueryConditionInfo;
import com.bupt.ilink.entity.pojo.store.UploadFlowInfo;
import com.bupt.ilink.entity.store.Download;
import com.bupt.ilink.entity.store.Like;
import com.bupt.ilink.mapper.FolderMapper;
import com.bupt.ilink.service.StoreService;
import com.bupt.ilink.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("store")
public class StoreController {

    @Autowired
    private StoreService storeService;

    @GetMapping("/check/")
    public R check() {
        return R.ok(storeService.check());
    }

    @PostMapping("/components/upload")
    public R uploadFlow(@RequestBody UploadFlowInfo flowInfo) {
        return R.ok(storeService.uploadFlow(flowInfo));
    }

    @GetMapping("/components/download")
    public R downloadFlow(Download download) {
        return R.ok(storeService.downloadFlow(download));
    }

    @PostMapping("/like")
    public R toggleLike(@RequestBody Like like) {
        return R.ok(storeService.toggleLike(like));
    }

    @GetMapping("/components/all")
    public R getAllFlows() {
        return R.ok(storeService.getAllFlows());
    }
    @PatchMapping("/components/update")
    public R updateFlow(@RequestBody UploadFlowInfo flowInfo) {
        return R.ok(storeService.updateFlow(flowInfo));
    }

    @GetMapping("/components/query")
    public R getFlowsByCondition(QueryConditionInfo condition) {
        return R.ok(storeService.getFlowsByCondition(condition));
    }
    @GetMapping("/tags")
    public R getTags() {
        return R.ok(storeService.getTags());
    }

    @Autowired
    FolderMapper folderMapper;
    @GetMapping("/folders")
    public R getFolders(@AuthenticationPrincipal UserDetails userDetails){
        User user = (User) userDetails;
        String userId= user.getUserId();
        return R.ok(folderMapper.selectByUserId(userId));
    }

}
